﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Packaging;

namespace Fireball.ImagePak
{
    public class ImageCollection : IEnumerable<Image>
    {
        private ImagePackage ImagePackage = null;

        internal ImageCollection(ImagePackage package)
        {
            ImagePackage = package;
        }

      //  private Uri MainUri = new Uri("/Images/Data/main.bin",  UriKind.Relative);

        public Image AddImage(string imageType)
        {
            var uri = CreateNewUri();
            var part = this.ImagePackage.CreatePackagePart(uri, imageType);
            Image image = new Image(part);
            Datas.Add(image);
            return image;
        }

        public Image AddMainImage(string imageType)
        {
            if (GetMain() != null)
            {
                throw new Exception("A main image already exists");
            }
            var uri = CreateNewUri();
            var part = this.ImagePackage.CreatePackagePart(this.ImagePackage.PackageInfo.MainImage, imageType);
            Image image = new Image(part);

            Datas.Add(image);
            return image;
        }

        internal void AddImageFromPackagePart(ZipPackagePart part)
        {
            Datas.Add(new Image(part));
        }

        private Uri CreateNewUri()
        {
            return new Uri("/Images/Data/" + Guid.NewGuid().ToString() + ".bin", UriKind.Relative);
        }

        //public void SetAsMain(Image imageData)
        //{
        //    if (this.Datas.Contains(imageData) == false)
        //    {
        //        throw new Exception("The specified image data is not present on the collection");
        //    }
        //    var cMain = GetMain();

        //    if (cMain != null)
        //    {
        //        cMain.SetUri(CreateNewUri());
        //    }

        //    imageData.SetUri(MainUri);
        //}

        public Image GetMain()
        {
            var r = from p in this where p.PackageUri == this.ImagePackage.PackageInfo.MainImage select p;

            if (r != null && r.Count() > 0)
            {
                return r.First();
            }

            return null;
        }


        private List<Image> Datas = new List<Image>();

        #region IEnumerable<ImageData> Members

        public IEnumerator<Image> GetEnumerator()
        {
            return Datas.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return Datas.GetEnumerator();
        }

        #endregion
    }
}
